package timing.jframe4l.common.config;

import com.alibaba.druid.util.DruidPasswordCallback;
import org.apache.commons.lang3.StringUtils;
import timing.jframe4l.common.utils.SecurityUtil;

import java.util.Properties;

/**
 * 数据库密码回调解密
 *  timing.jframe4l.common.config.DBPasswordCallback
 * @author 吕自聪
 * @version 2016年5月20日 下午3:18:25
 */
@SuppressWarnings("serial")
public class DBPasswordCallback extends DruidPasswordCallback {
	private static final byte[] key = {9, -1, 0, 5, 39, 8, 6, 19};

	public void setProperties(Properties properties) {
		super.setProperties(properties);
		String pwd = properties.getProperty("password");
		if (StringUtils.isNotBlank(pwd)) {
			try {
				String password = SecurityUtil.decryptDes(pwd, key);
				setPassword(password.toCharArray());
			} catch (Exception e) {
				setPassword(pwd.toCharArray());
			}
		} else {
			setPassword("".toCharArray());
		}
	}

	/**
	 * 请使用该方法加密后，把密文写入classpath:/config/jdbc.properties
	 * 具体如何构建才能生成可执行的jar包，请参见 mob-core项目下的pom文件
	 * bsoft:JfH7YkJz65q=
	 * admin:WFU1Hfh+W8q=
	 * root:sOGhhAUYuM7=
	 * */
	public static void main(String[] args) {
		if (args.length > 0) {
			for (int i = 0; i < args.length; i++) {
				String encrypt = SecurityUtil.encryptDes(args[i], key);
				System.out.println(encrypt);
				System.out.println(SecurityUtil.decryptDes(encrypt, key));
			}
		} else {
			String encrypt = SecurityUtil.encryptDes("root", key);
			System.out.println(encrypt);
			System.out.println(SecurityUtil.decryptDes(encrypt, key));
		}

	}
}
